﻿
#if (defined(__EFFEKSEER_NETWORK_ENABLED__))
#if !(defined(_PSVITA) || defined(_XBOXONE))

#include "Effekseer.Socket.h"

#if defined(_WIN32) && !defined(_PS4)
#else
#include <sys/socket.h>
#include <sys/types.h>
#endif

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
namespace Effekseer
{
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void Socket::Initialize()
{
#if defined(_WIN32) && !defined(_PS4)
	// Initialize  Winsock
	WSADATA m_WsaData;
	::WSAStartup(MAKEWORD(2, 0), &m_WsaData);
#endif
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void Socket::Finalize()
{
#if defined(_WIN32) && !defined(_PS4)
	// Dispose winsock or decrease a counter
	WSACleanup();
#endif
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
EfkSocket Socket::GenSocket()
{
	return ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void Socket::Close(EfkSocket s)
{
#if defined(_WIN32) && !defined(_PS4)
	::closesocket(s);
#else
	::close(s);
#endif
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void Socket::Shutsown(EfkSocket s)
{
#if defined(_WIN32) && !defined(_PS4)
	::shutdown(s, SD_BOTH);
#else
	::shutdown(s, SHUT_RDWR);
#endif
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
bool Socket::Listen(EfkSocket s, int32_t backlog)
{
#if defined(_WIN32) && !defined(_PS4)
	return ::listen(s, backlog) != SocketError;
#else
	return listen(s, backlog) >= 0;
#endif
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
} // namespace Effekseer
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------

#endif // #if !( defined(_PSVITA) || defined(_XBOXONE) )
#endif